export function searchColumns() {
  return [
    {
      filterable: true,
      type: 'select',
      dicData: this.channelList?.list,
      props: {
        label: 'no',
        value: 'no',
      },
      prop: 'channel',
      placeholder: '请选择渠道',
    },
  ]
}

export function tableColumns() {
  return [
    {
      width: 100,
      label: '序号',
      prop: 'id',
    },
    {
      width: 120,
      label: '渠道',
      prop: 'channel',
      headerslot: true,
    },
    {
      width: 200,
      overHidden: true,
      align: 'left',
      label: '文件名',
      prop: 'filename',
    },
    {
      width: 120,
      label: '软件版本',
      prop: 'version',
    },
    {
      width: 120,
      label: '软件号',
      prop: 'version_no',
    },
    {
      align: 'left',
      width: 120,
      label: '更新标题',
      prop: 'title',
    },
    {
      align: 'left',
      overHidden: true,
      width: 200,
      label: '内容',
      prop: 'description',
    },
    {
      width: 200,
      align: 'left',
      label: '安装包下载链接',
      prop: 'url',
    },
    {
      width: 140,
      label: '安装包大小',
      prop: 'size',
      formatter: row => this.$formatFlowSize(row?.size),
    },
    {
      width: 140,
      label: '升级时间',
      prop: 'updated_at',
      formatter: row => this.$formatTime(row?.updated_at),
    },
    {
      width: 100,
      label: '是否强制更新',
      prop: 'force_update',
      html: true,
      formatter: row =>
        `<span class="${row?.force_update === 1 ? 'text-success' : 'text-warning'}">${
          row?.force_update === 1 ? '是' : '否'
        }</span>`,
    },
  ]
}
export function addDialogForm() {
  const validateWeight = (rule, data, callback) => {
    const versionRegex = /^[0-9]\.[0-9]\.[0-9]$/

    if (!data) {
      callback(new Error('请输入版本'))
    }
    if (!versionRegex.test(data)) {
      callback(new Error('格式要求为:1.0.0'))
    }
    callback()
  }
  const validateUpload = (rule, data, callback) => {
    if (data.length === 0) {
      callback(new Error('请上传文件'))
    }
    if (data.length !== 0 && data[0]?.progress !== 100) {
      callback(new Error('文件未上传完毕'))
    }
    callback()
  }
  return [
    {
      filterable: true,
      type: 'select',
      dicData: this.channelList?.list,
      props: {
        label: 'no',
        value: 'no',
      },
      label: '渠道',
      prop: 'channel',
    },

    {
      label: '版本',
      prop: 'version',
      rules: [
        {
          required: true,
          validator: validateWeight,
        },
      ],
    },
    {
      type: 'number',
      label: '版本号',
      prop: 'version_no',
    },
    {
      label: '更新标题',
      prop: 'title',
    },
    {
      type: 'radio',
      label: '是否强制更新',
      prop: 'force_update',
      value: 1,
      dicData: [
        { value: 1, label: '是' },
        { value: 2, label: '否' },
      ],
    },
    {
      type: 'textarea',
      label: '内容',
      prop: 'description',
    },
    {
      label: '上传文件',
      prop: 'film_apk',
      component: 'UploadFile',
      params: {
        maxSize: '100M',
        multiple: false,
        fileTypes: ['apk'],
        params: {
          dir: 'apk',
          ty: 'oss',
        },
      },
      rules: [
        {
          required: true,
          validator: validateUpload,
        },
      ],
    },
  ]
}
function heapSort(array) {
  creatHeap(array)
  console.log(array)
  // 交换第一个和最后一个元素，然后重新调整大顶堆
  for (let i = array.length - 1; i > 0; i--) {
    ;[array[i], array[0]] = [array[0], array[i]]
    adjust(array, 0, i)
  }
  return array
}
// 构建大顶堆，从第一个非叶子节点开始，进行下沉操作
function creatHeap(array) {
  const len = array.length
  const start = parseInt(len / 2) - 1
  for (let i = start; i >= 0; i--) {
    adjust(array, i, len)
  }
}
// 将第target个元素进行下沉，孩子节点有比他大的就下沉
function adjust(array, target, len) {
  for (let i = 2 * target + 1; i < len; i = 2 * i + 1) {
    // 找到孩子节点中最大的
    if (i + 1 < len && array[i + 1] > array[i]) {
      i = i + 1
    }
    // 下沉
    if (array[i] > array[target]) {
      ;[array[i], array[target]] = [array[target], array[i]]
      target = i
    } else {
      break
    }
  }
}
